const express = require('express');
const  router = express.Router();

// 添加商品
router.post('/add' ,async (req,res)=>{
    const { Goods }  = req.Model;
    
    let {
        name,         //名字
        price,        //价格  
        purchasePrice, //入货价
        inventory,   //库存
        title,       //标题
        detail,     //描述
        time,
        volume=0,
    }  = req.body;
    let userId = req.headers.authorization
    const params = {}
    params.name = name
    params.userId = userId
     // 数据过滤的操作 

    let goods = await Goods.findOne(params) 
    if(goods) {
        return  res.send({code:500,info:'商品已存在!'})
    }else {
        try {
            await Goods.create({
                name,         //名字
                title,       //标题
                inventory,   //库存
                price,        //价格  
                purchasePrice, //入货价
                detail,     //描述
                userId,     //用户Id
                time,
                volume,
            })
            res.send({code:200,info:'添加成功'})
        } catch(e) {
        //    console.log('eeee',e)
            res.send({code:500,info:'添加失败'})
        }
    }
})

//修改商品
router.post('/edit' ,async (req,res)=>{
    const { Goods }  = req.Model;
       // 数据过滤的操作 
    const { Goodsid ,...updateData} = req.body;
    // console.log('name',updateData.name)
    try {
        await Goods.findByIdAndUpdate(Goodsid,updateData);
        res.send({code:200,info:'修改成功'})
    } catch(e) {
        res.send({code:500,info:'修改失败'})
    }
  
})

//修改商品销量
router.post('/editVolume' ,async (req,res)=>{
    const { Goods }  = req.Model;
       // 数据过滤的操作 
    const { Goodsid , name,inventory,volume,goodsVolume} = req.body;
    const updateData = {}
    updateData.name = name
    updateData.inventory = inventory - goodsVolume
    updateData.volume = volume + goodsVolume

    try {

        await Goods.findByIdAndUpdate(Goodsid,updateData);
        res.send({code:200,info:'出库成功'})
    } catch(e) {
        res.send({code:500,info:'出库失败'})
    }
  
})

// 分页查询
router.post('/getAll', async (req,res)=>{

    const { Goods} = req.Model;
    let { current=1,limit=10,value } = req.body;
    let userId = req.headers.authorization
    const skip =  (current - 1 ) * limit; // 查询的起点（偏移量）
    const params = {}
    params.userId = userId
    if(value) params.name = new RegExp(value, 'i');
    try {

       let goods = await Goods.find(params,{},{skip,limit}).sort({_id: 1}) // 分页查询
      
       let num = await Goods.find(params,{},{})
       let count = num.length
    //    let count = await Goods.count() // 获取符合条件的总数
        // let count = goods.length

       res.send({code:200,info:'查询成功',data:goods,count});
       
    }catch(e){
       res.send({code:500,info:'获取失败'})
    }

})

// 查询单个商品信息
router.post('/getOne', async (req,res)=>{
    const { Goods} = req.Model;
  
    let { goodsid } = req.body;
  
    if(!goodsid) return res.send({code:500,info:'该商品不存在'})
  
    try {
  
      let goods = await Goods.findById(goodsid) // 分页查询
     
      res.send({code:200,info:'查询成功',data:goods});
      
    }catch(e){
      res.send({code:500,info:'获取失败'})
    }
  })

router.post('/del', async (req,res)=>{
    const { Goods } = req.Model;
    let { goodsid } = req.body;
  
    if(!goodsid) return res.send({success:false,info:'该商品不存在'})
  
    try {
  
      await Goods.findByIdAndRemove(goodsid) 
     
      res.send({code:200,info:'删除成功'});
      
    }catch(e){
  
      res.send({code:500,info:'删除失败'})
    }
})
module.exports = router;
